package com.hulu.yec.teach.examrecords.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;





import com.hulu.yec.common.Constant;
import com.hulu.yec.common.PageModel;
import com.hulu.yec.common.util.LogUtil;
import com.hulu.yec.sys.user.service.SysUserDao;
import com.hulu.yec.teach.examrecords.entity.ExamRecordsEntity;
import com.hulu.yec.teach.examrecords.entity.RecordCondition;
import com.hulu.yec.teach.examrecords.service.ExamRecordDao;

@WebServlet("/teach/examrecords/ExamRecordsServlet")
public class ExamRecordsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static final ExamRecordDao examRecordDao = new ExamRecordDao(); 

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding(Constant.ENCODING_UTF8);
		String command = request.getParameter("command");
		
		
		if("list".equals(command)){
			list(request,response);
			
			
			
		}
	}

	/**
	 * 分页带条件查询考试记录
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RecordCondition recordCondition = new RecordCondition();
		int pageNo = Constant.DEFAULT_PAGE_NO;
		int pageSize = Constant.DEFAULT_PAGE_SIZE;
		setConditon(recordCondition,request,response,pageNo,pageSize);
		PageModel<ExamRecordsEntity> pageModel= examRecordDao.examrelist(recordCondition, pageNo, pageSize);
		request.setAttribute("pageModel", pageModel);
		LogUtil.i(Constant.LOGGER, pageModel.getList().toString());
		request.setAttribute("researchInfo", recordCondition);
		request.getRequestDispatcher("/WEB-INF/view/teach/records/list-records.jsp").forward(request, response);
	}

	/**
	 * 设置前台参数，封装实体
	 * 
	 * @param recordCondition
	 * @param request
	 * @param response
	 * @param pageNo
	 * @param pageSize
	 */
	private void setConditon(RecordCondition recordCondition,
			HttpServletRequest request, HttpServletResponse response,int pageNo,int pageSize) {
		String pageNoStr = request.getParameter("pageNo");
		String pageSizeStr = request.getParameter("pageSize");
		if(StringUtils.isNoneBlank(pageNoStr)){
			pageNo = Integer.parseInt(pageNoStr);
		}
		if(StringUtils.isNoneBlank(pageSizeStr)){
			pageSize = Integer.parseInt(pageSizeStr);
		}
		recordCondition.setUserName(request.getParameter("userName"));
		recordCondition.setClassName(request.getParameter("className"));
		recordCondition.setPaperName(request.getParameter("paperName"));
		String stateStr = request.getParameter("state");
		String startPoint = request.getParameter("startPoint");
		String endPoint = request.getParameter("endPoint");
		if(StringUtils.isNoneBlank(stateStr)){
			recordCondition.setState(Integer.parseInt(stateStr));
		}
		if(StringUtils.isNoneBlank(startPoint)){
			recordCondition.setStartPoint(Integer.parseInt(startPoint));
		}
		if(StringUtils.isNoneBlank(endPoint)){
			recordCondition.setEndPoint(Integer.parseInt(endPoint));
		}
		
		recordCondition.setClassList(new SysUserDao().getClassList());
		
	}

}
